package com.tubruk.trackme;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class MyRouteOverlay extends Overlay {
	private MyGeoPoint prevPoint;
	private MyGeoPoint currPoint;
	private List<MyGeoPoint> myGeoPoints;
	private int mRadius = 4;
	private int mode = 0;
	private int defaultColor;
	private String text = "";
	private MapView mapView;
	private boolean isDrawn = false;
	
	public MyRouteOverlay(List<MyGeoPoint> myGP){
		this.myGeoPoints = myGP;
		this.mode = 2;
	}
	
	public void setPoints(List<MyGeoPoint> myGP, int mode){
		this.myGeoPoints = myGP;
		this.mode = mode;
	}
	
	public MyRouteOverlay(List<MyGeoPoint> myGP, int mode) {
		this.myGeoPoints = myGP;
		this.mode = mode;
	}
	
	public MyRouteOverlay(MyGeoPoint gp1, MyGeoPoint gp2, int mode) {
		//no default color
		this(gp1, gp2, mode, 999);
	}

	public MyRouteOverlay(MyGeoPoint gp1, MyGeoPoint gp2, int mode, int defaultColor) {
		this.prevPoint = gp1;
		this.currPoint = gp2;
		this.mode = mode;
		this.defaultColor = defaultColor;
	}

	public void setText(String t) {
		this.text = t;
	}
	
	public void setMode(int mode) {
		this.mode = mode;
	}

	public int getMode() {
		return mode;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		if (!shadow && myGeoPoints != null && myGeoPoints.size() > 0) {
			Projection projection = mapView.getProjection();
			isDrawn = true;
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setDither(true);
	        paint.setColor(Color.RED);
	        paint.setStyle(Paint.Style.FILL_AND_STROKE);
	        paint.setStrokeJoin(Paint.Join.ROUND);
	        paint.setStrokeCap(Paint.Cap.ROUND);
	        paint.setStrokeWidth(2);
			
	        int idx = 0;
	        int size = myGeoPoints.size() - 1;
	        
	        Point point = new Point();
	        Point point2 = new Point();
	        Log.d("DRAW PREP", "Draw for " + (size + 1) + " points");
	        for (MyGeoPoint myg: myGeoPoints) {
	        	//setup up points and projection
	        	currPoint = myg;
	        	projection.toPixels(currPoint, point2);
	        	Log.d("DRAW", currPoint.getTimeStart() + "");
	        	Path mPath = new Path();
	        	
	        	if (idx == 0) {
	        		//start node
	        		paint.setColor(Color.BLUE);
	        		Log.d("DRAW", "Mode 1");
	        		RectF oval = new RectF(point2.x - mRadius, point2.y - mRadius,
							point2.x + mRadius, point2.y + mRadius);
	        		mPath.addOval(oval, Path.Direction.CW);
	        	} else if (idx == size && mode == 3) {
	        		//end node
	        		paint.setColor(Color.BLUE);
	        		Log.d("DRAW", "Mode 3");
	        		RectF oval = new RectF(point2.x - mRadius, point2.y - mRadius,
							point2.x + mRadius, point2.y + mRadius);
	        		mPath.moveTo(point.x, point.y);
	        		mPath.lineTo(point2.x, point2.y);
	        		mPath.addOval(oval, Path.Direction.CW);
	        	} else {
	        		//ongoing path
	        		if (idx > 0) {
	        			prevPoint = myGeoPoints.get(idx - 1);
	        		}
	        		projection.toPixels(prevPoint, point);
	        		Log.d("DRAW", "Mode 2");
	        		
	        		paint.setColor(Color.RED);
	        		mPath.moveTo(point.x, point.y);
					mPath.lineTo(point2.x, point2.y);
	        	}
	        	idx++;
	        	canvas.drawPath(mPath, paint);
	        }
		}
	}

	public MyGeoPoint getPrevPoint() {
		return prevPoint;
	}

	public MyGeoPoint getCurrPoint() {
		return currPoint;
	}
}
